package admin.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import models.StaffModel;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class StaffData {
	private Objectify ofy;

	private Cache cache = null;

	private String prefix = "staff_";

	private int limit = 20;

	private int page = 1;

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public StaffData() {
		try {
			ObjectifyService.register(StaffModel.class);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			cache = CacheManager.getInstance().getCacheFactory()
					.createCache(Collections.emptyMap());
		} catch (CacheException e) {
			// e.printStackTrace();
		}

		ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
		ofy = ObjectifyService.begin(opts);
	}

	public ArrayList<StaffModel> getAll() {
		ArrayList<StaffModel> staffs = new ArrayList<StaffModel>();

		Query<StaffModel> q = ofy.query(StaffModel.class);
		for (StaffModel staff : q) {
			staffs.add(staff);
		}

		return staffs;
	}
	
	public ArrayList<StaffModel> list() {
		ArrayList<StaffModel> staffs = new ArrayList<StaffModel>();

		int start = (page - 1) * limit;
		Query<StaffModel> q = ofy.query(StaffModel.class).limit(limit)
				.offset(start);
		for (StaffModel staff : q) {
			staffs.add(staff);
		}

		return staffs;
	}

	@SuppressWarnings("unchecked")
	public void insert(StaffModel staff) {
		if (cache != null) {
			cache.put(prefix + staff.getUsername(), staff);
		}
		ofy.put(staff);
	}

	public void delete(StaffModel staff) {
		if (cache != null && cache.containsKey(prefix + staff.getUsername())) {
			cache.remove(prefix + staff.getUsername());
		}

		ofy.delete(staff);
	}

	@SuppressWarnings("unchecked")
	public StaffModel getStaff(String username) {
		StaffModel staff = new StaffModel();
		if (cache != null && cache.containsKey(prefix + username)) {
			staff = (StaffModel) cache.get(prefix + username);
		} else {
			try {
				staff = ofy
						.get(new Key<StaffModel>(StaffModel.class, username));
				cache.put(prefix + username, staff);
			} catch (Exception e) {
				staff = null;
			}
		}
		return staff;
	}

	public boolean checkStaffExists(String username) {
		boolean exists = false;
		StaffModel staff = getStaff(username);
		if (staff != null) {
			exists = true;
		}
		return exists;
	}

	public List<StaffModel> getListByRole(int role) {
		List<StaffModel> staffs = new ArrayList<StaffModel>();
		Query<StaffModel> q = ofy.query(StaffModel.class).filter("role", role);
		for (StaffModel staff : q) {
			staffs.add(staff);
		}
		return staffs;
	}

	public List<StaffModel> getActiveListByRole(int role) {		
		List<StaffModel> staffs = new ArrayList<StaffModel>();
		Query<StaffModel> q = ofy.query(StaffModel.class).filter("role", role)
				.filter("status", 2);
		for (StaffModel staff : q) {			
			staffs.add(staff);
		}		
		return staffs;
	}

	public StaffModel getByCode(String code) {
		StaffModel staff = new StaffModel();
		Query<StaffModel> q = ofy.query(StaffModel.class).filter("code", code);
		for (StaffModel sta : q) {
			staff = sta;
		}
		return staff;
	}
}